import { getToken, setToken, removeToken } from '@/utils/auth'
import { login, getInfo, logoutdata } from '@/api/user'
import { setTime } from '@/utils/auth'
import { resetRouter } from '@/router/index'
// import { resetRouter } from '@/store/index'

const state = {
  token: getToken() || '', // 设置token,获取本地token
  userInfo: {}
  // staffphoto: ''
}
const mutations = {
  setTokenFn(state, token) {
    state.token = token
    setToken(token)
  },
  removetokenFn(state) {
    // 清空数组
    state.token = null
    // 清空本地的token
    removeToken()
  },
  setuserInfo(state, data) {
    state.userInfo = data
  },
  removeuserInfo(state) {
    state.userInfo = {}
  }

}
const actions = {
  async loginFn(store, dataFn) {
    const data = await login(dataFn)
    // console.log(data)
    store.commit('setTokenFn', data)
    setTime()
  },

  async userInfoFn(store) {
    const res = await getInfo()
    const datail = await logoutdata(res.userId)

    console.log(datail)
    // console.log(res)
    store.commit('setuserInfo', {
      ...res,
      ...datail
    })
  },
  logout(store) {
    resetRouter()
    store.commit('removetokenFn')
    store.commit('removeuserInfo')
  }

}

export default {
  namespaced: true,
  state,
  mutations,
  actions

}
